\lr<115>Collision Detection Between Animation Objects\fs<12>
\fs<14>by Ken Farinsky\fs<12>
\bCollision detection allows the system to manage Gel-to-Border and Gel-to-Gel collision handling for an application. "Gels" are VSprite-based objects that may be displayed and moved about the Amiga display. These objects include VSprites (Virtual Sprites), Bobs (Blitter Objects), and AnimObs (Animation Objects). For a detailed discussion of collision detection between simple Gels (such as VSprites and Bobs), see the "Graphics: Sprites, Bobs and Animation" chapter in the \IROM Kernel Manual: Libraries and Devices\i.
The basic premise of Amiga collision detection is to call a user supplied collision routine when two objects overlap. This is complicated by two further enhancements. First, borders are counted as objects and special-case processing of collisions is provided between Gels and the border. Second, each Gel has two masks that indicate how pairs of objects will react when they collide.
Collision detection between objects such as VSprites and Bobs is conceptually very simple. Each object is built upon a single underlying structure, the VSprite structure. After the Gel has been added to the system, the DoCollision() routine scans the list of VSprites for overlapping objects and calls the appropriate user-supplied collision routine in the event of a collision. Since there is a one-to-one correspondence between VSprites and objects, it is obvious which VSprite structure belongs to which object on the screen.
\BAnimation Objects Are Not Simple Gels\b
Animation Objects contain multiple VSprite structures which are linked to form a larger object. When the object is being animated, the system cycles through these underlying components, displaying each at the appropriate moment. At any given point in time the system may be in the process of removing the last displayed component and adding in the next one. During such a component switch, the system will have two VSprites added for a single object: one is in the process of being removed and one is in the process of being displayed.
\BSpecial Initialization Steps for Complex Gel Collision Detection
When setting-up a multi-component Gel, each underlying VSprite structure must be initialized for collision, as the system will not look to the others for more information. An AnimOb's collision detection information need not be static. The collision information can change from frame to frame in the same manner image data can change. The example shows a special case, where all frames are similar enough that they have identical collision information.
The following fields in the VSprite structure must be properly initialized for system collision detection: HitMask, MeMask, BorderLine and CollMask. HitMask and MeMask tell the system which collision routine to call when a collision is detected. BorderLine and CollMask are "shadow masks" that are used to determine if this component overlaps any other object. These values are set just as they would be for a single VSprite or a Bob.
Note that it is impossible for components of a single AnimOb's sequence to collide with one another. However, in an AnimOb that contains multiple sequences, it is possible for a component in one sequence to collide with a component from a different sequence. This can be prevented through careful selection of HitMask and MeMask values, or can be detected in the individual collision routines.
\BWhat Happens When Complex Gels Collide
Collisions between Animation Objects \Ishould\i be fairly simple to process. The DoCollision() routine \Ishould\i examine all displayed objects for both overlap of objects and for correct interaction between the HitMask and MeMask, then call the appropriate collision routine.\lr<120>
Unfortunately, the DoCollision() routine does not ignore the previous AnimOb component, which is in the process of being removed from the display. These components are moved by the system to a position well above and to the left of the visible display. When the Gels are next drawn, the removed component will be moved off-screen, causing it to disappear from the display. At the same time, the new component will be drawn on-screen. This would not be a problem if the removed components did not overlap, however, all components that are being removed are positioned at the same off-screen location. This means that they all overlap and collisions will occur between all pairs of components that are in the process of being removed.
\BThe \ff<Courier>BOBSAWAY\ff<Times> Flag Comes to the Rescue\b
To handle collisions between pairs of AnimObs, the Gel-to-Gel collision routines must ignore the objects that are being removed. There are two ways to handle this. The first is to check for large negative position values in the X and Y variables of the VSprite structure. This works, but is not the best way to check for removal. The second, and preferred method, is to look at the flags of the associated Bob. If the \ff<Courier>BOBSAWAY\ff<Times> flag is set, then the bob is being removed and should not be used in the collision routines. Note that this has been changed for 2.0, which will only look at active VSprites for collision detection.
The example code is made up of four files:
1)\sanimtools.h \s\s- include file necessary to use animtools.
2)\sanimtools_proto.h\s\s- prototypes for animtools functions.
3)\sanimtools.c \s\s- animtools functions. These simplify the use of the animation
\s\s\s\s\s\s\s\ssub-system.
4)\scollide.c \s\s\s- example code.
The example code was published in the latest release of the \IROM Kernel Manual: Libraries & Devices\i. It has been updated and reprinted here. The example can be compiled and linked using Lattice C with these commands: